package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.NewDateUtils;
import com.ruoyi.system.domain.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AlpYingshouMapper;
import com.ruoyi.system.service.IAlpYingshouService;
import com.ruoyi.common.core.text.Convert;

/**
 * 应收表Service业务层处理
 * 
 * @author zhangjun
 * @date 2020-02-14
 */
@Service
public class AlpYingshouServiceImpl implements IAlpYingshouService 
{
    @Autowired
    private AlpYingshouMapper alpYingshouMapper;

    /**
     * 查询应收表
     * 
     * @param id 应收表ID
     * @return 应收表
     */
    @Override
    public AlpYingshou selectAlpYingshouById(Long id)
    {
        return alpYingshouMapper.selectAlpYingshouById(id);
    }

    /**
     * 查询应收表列表
     * 
     * @param alpYingshou 应收表
     * @return 应收表
     */
    @Override
    public List<AlpYingshou> selectAlpYingshouList(AlpYingshou alpYingshou)
    {
        return alpYingshouMapper.selectAlpYingshouList(alpYingshou);
    }

    /**
     * 新增应收表
     * 
     * @param alpYingshou 应收表
     * @return 结果
     */
    @Override
    public int insertAlpYingshou(AlpYingshou alpYingshou)
    {
        return alpYingshouMapper.insertAlpYingshou(alpYingshou);
    }

    /**
     * 修改应收表
     * 
     * @param alpYingshou 应收表
     * @return 结果
     */
    @Override
    public int updateAlpYingshou(AlpYingshou alpYingshou)
    {
        return alpYingshouMapper.updateAlpYingshou(alpYingshou);
    }

    /**
     * 删除应收表对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAlpYingshouByIds(String ids)
    {
        return alpYingshouMapper.deleteAlpYingshouByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除应收表信息
     * 
     * @param id 应收表ID
     * @return 结果
     */
    @Override
    public int deleteAlpYingshouById(Long id)
    {
        return alpYingshouMapper.deleteAlpYingshouById(id);
    }

    @Override
    public List<AlpShowActual> showActualState()
    {
        return alpYingshouMapper.showActualState();
    }


    @Override
    public BigDecimal calcQuarterSumBudget(Integer num)
    {
        return alpYingshouMapper.calcQuarterSumBudget(num);
    }

    @Override
    public BigDecimal calcQuarterSumPredict(Integer num)
    {
        return alpYingshouMapper.calcQuarterSumPredict(num);
    }

    @Override
    public BigDecimal calcQuarterSumActual(Integer num)
    {
        return alpYingshouMapper.calcQuarterSumActual(num);
    }


    @Override
    public Map<String, List<BigDecimal>> calcQuarterSum() {
        Map<String, List<BigDecimal>> resultMap = new LinkedHashMap<>();
        for (int i = 1; i<=4; i++) {

            Calendar cal = Calendar.getInstance();
            int quarter = DateUtils.getQuarter(new Date());
            BigDecimal predict;
            if(i <= quarter){ //当前月份之前的预测数据，从alp_predict_quarter表中取数据，之后的数据，从alp_stage_predict表中取数据
                predict = alpYingshouMapper.calcEffQuarterSumPredict(i);
            } else {
                predict = alpYingshouMapper.calcQuarterSumPredict(i);
            }
            List<BigDecimal> list = new ArrayList<>();
            BigDecimal budget = alpYingshouMapper.calcQuarterSumBudget(i);
            BigDecimal actual = alpYingshouMapper.calcQuarterSumActual(i);
            list.add(budget == null ? BigDecimal.ZERO: budget);
            list.add(predict == null ? BigDecimal.ZERO: predict);
            list.add(actual == null ? BigDecimal.ZERO: actual);
            resultMap.put(i+"季度", list);
        }
        return resultMap;
    }

    @Override
    public List<EveryOneBooking> everyoneSumBooking() {
        return alpYingshouMapper.everyoneSumBooking();
    }

    @Override
    public Map<String, List<BigDecimal>> calcMonthSum() {
        Map<String, List<BigDecimal>> resultMap = new LinkedHashMap<>();
        for (int i = 1; i<=12; i++) {
            Calendar cal = Calendar.getInstance();
            int month = cal.get(Calendar.MONTH) + 1;
            BigDecimal predict;
            if(i <= month){ //当前月份之前的预测数据，从alp_predict_month表中取数据，之后的数据，从alp_stage_predict表中取数据
                predict = alpYingshouMapper.calcEffMonthSumPredict(i);
            } else {
                predict = alpYingshouMapper.calcMonthSumPredict(i);
            }
            List<BigDecimal> list = new ArrayList<>();
            BigDecimal budget = alpYingshouMapper.calcMonthSumBudget(i);
            BigDecimal actual = alpYingshouMapper.calcMonthSumActual(i);
            list.add(budget == null ? BigDecimal.ZERO: budget);
            list.add(predict == null ? BigDecimal.ZERO: predict);
            list.add(actual == null ? BigDecimal.ZERO: actual);
            resultMap.put(i+"月", list);
        }
        return resultMap;
    }

    @Override
    public Map<String, List<BigDecimal>> calcWeekSum() {
        Map<String, List<BigDecimal>> resultMap = new LinkedHashMap<>();
        for (int i = 1; i<=53; i++) {
            int week = DateUtils.getWeekOfYearByF(new Date(), 5);
            BigDecimal predict;
            if(i <= week){ //当前周之前的预测数据，从alp_predict_week表中取数据，之后的数据，从alp_stage_predict表中取数据
                predict = alpYingshouMapper.calcEffWeekSumPredict(i);
            } else {
                predict = alpYingshouMapper.calcWeekSumPredict(i);
            }

            List<BigDecimal> list = new ArrayList<>();
            BigDecimal budget = alpYingshouMapper.calcWeekSumBudget(i);
            BigDecimal actual = alpYingshouMapper.calcWeekSumActual(i);
            list.add(budget == null ? BigDecimal.ZERO: budget);
            list.add(predict == null ? BigDecimal.ZERO: predict);
            list.add(actual == null ? BigDecimal.ZERO: actual);
            resultMap.put(i+"周", list);
        }
        return resultMap;
    }

    @Override
    public Map<String, BigDecimal> calcBPAatNow(){
        Map<String, BigDecimal> resultMap = new LinkedHashMap<>();

        Date now = new Date();
        Integer weekNum = NewDateUtils.getWeek(now);
        BigDecimal predictWeek = alpYingshouMapper.calcWeekSumPredict(weekNum);
        Integer monthNum = NewDateUtils.getMonth(now);
        BigDecimal predictMonth = alpYingshouMapper.calcMonthSumPredict(monthNum);
        Integer QuarterNum = NewDateUtils.getQuarter(now);
        BigDecimal predictQuarter = alpYingshouMapper.calcQuarterSumPredict(QuarterNum);
        String year = DateUtils.dateTimeNow(DateUtils.YYYY);
        BigDecimal budgetYear = alpYingshouMapper.calcYearSumBudget(Integer.valueOf(year));
        BigDecimal predictYear = alpYingshouMapper.calcYearSumPredict(Integer.valueOf(year));
        BigDecimal actualYear = alpYingshouMapper.calcYearSumActual(Integer.valueOf(year));
        resultMap.put("本周预测总额", predictWeek);
        resultMap.put("本月预测总额", predictMonth);
        resultMap.put("本季预测总额", predictQuarter);
        resultMap.put("本年预算总额", budgetYear);
        resultMap.put("本年预测总额", predictYear);
        resultMap.put("本年实际总额", actualYear);
        return resultMap;
    }


    @Override
    public List<ContractSheet> calcContractSheet(){
        List<ContractSheet> contractSheets = alpYingshouMapper.calcContractSheet();
        //补零
        for (ContractSheet contractSheet : contractSheets) {
            if(null == contractSheet.getContractCount()) {
                contractSheet.setContractCount(0);
            }
            if(null == contractSheet.getContractNumber()) {
                contractSheet.setContractNumber(BigDecimal.ZERO);
            }
        }
        return contractSheets;
    }

    //计算逾期合同
    @Override
    public List<OverDueContract> calcOverDueContract(){
        return alpYingshouMapper.calcOverDueContract();
    }

    @Override
    public List<HashMap> receivablesContractDetail(AlpContractDetail alpContractDetail) {
        return alpYingshouMapper.receivablesContractDetail(alpContractDetail);
    }

    @Override
    public List<HashMap> incompleteContractDetail(AlpContractDetail alpContractDetail) {
        return alpYingshouMapper.incompleteContractDetail(alpContractDetail);
    }

    @Override
    public List<HashMap> overdueContractDetail(AlpContractDetail alpContractDetail) {
        return alpYingshouMapper.overdueContractDetail(alpContractDetail);
    }

    @Override
    public List<HashMap> receivablesStageList(Long contractId) {
        return alpYingshouMapper.receivablesStageList(contractId);
    }

    @Override
    public BigDecimal calcEachMonthSumBudget(Integer month, Long contractId, Integer year) {
        return alpYingshouMapper.calcEachMonthSumBudget(month,contractId,year);
    }

    @Override
    public BigDecimal calcEachMonthSumPredict(Integer month, Long contractId, Integer year) {
        return alpYingshouMapper.calcEachMonthSumPredict(month,contractId,year);
    }

    @Override
    public BigDecimal calcEachMonthSumActual(Integer month, Long contractId, Integer year) {
        return alpYingshouMapper.calcEachMonthSumActual(month,contractId, year);
    }

    @Override
    public BigDecimal calcEachWeekSumBudget(Integer week, Long contractId, Integer year) {
        return alpYingshouMapper.calcEachWeekSumBudget(week,contractId,year);
    }

    public BigDecimal calcEachWeekSumPredict(Integer week, Long contractId, Integer year){
        return alpYingshouMapper.calcEachWeekSumPredict(week,contractId,year);
    }

    public BigDecimal calcEachWeekSumActual(Integer week, Long contractId, Integer year){
        return alpYingshouMapper.calcEachWeekSumActual(week,contractId,year);
    }
}
